package com.hmz.backend.config;

import com.hmz.backend.filter.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor myInterceptor;
    @Value("${my-server-address}")
    private String myServerAddress;
    @Value("${avatar-path}")
    private  String avatarPath ;
    @Value("${message-img-path}")
    private  String messageImgPath;
    @Value("${get-avatar-path}")
    private String getAvatarPath;
    @Value("${get-message-img-path}")
    private String getMessageImgPath;
    //添加跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 对所有路径生效
                .allowedOrigins("*") //允许所有源地址
                // .allowedOrigins("https://mijiupro.com","https://mijiu.com ") // 允许的源地址（数组）
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
                .allowedHeaders("*"); // 允许的请求头
    }
    //手写拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //往拦截器注册器里添加拦截器
        registry.addInterceptor(myInterceptor);
    }
    //添加资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(getAvatarPath+"/**").addResourceLocations("file:"+avatarPath); //头像
        registry.addResourceHandler(getMessageImgPath+"/**").addResourceLocations("file:"+messageImgPath); //消息图片
    }
}